﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LTM_Extra;

namespace LTM_Main
{
    public partial class ManagingVariables : Form
    {
        public ManagingVariables()
        {
            InitializeComponent();
        }

        ContentProvider contentProvider = new ContentProvider();

        private void ManagingVariables_Load(object sender, EventArgs e)
        {
            try
            {

                cmbVariableTemplateCategory.DataSource = contentProvider.GetWordTemplateCategories();
                cmbVariableType.DataSource = Constants.VariableType.GetList();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }

        private void cmbVariableTemplateCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                cmbVariableTemplates.DataSource = contentProvider.GetWordTemplatesByCategory(cmbVariableTemplateCategory.SelectedValue.ToString());
                cmbVariableTemplates.DisplayMember = "fileName";
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }

        private void cmbVariableTemplates_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                lstVariables.DataSource = contentProvider.GetVariableNamesFromSelectedTemplate(cmbVariableTemplateCategory.SelectedValue.ToString(), ((TableOfContentsWordCategoriesTemplates)(cmbVariableTemplates.SelectedValue)).fileName);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }

        private void lstVariables_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var variable = contentProvider.GetVariablePropertiesFormSelectedTemplate(cmbVariableTemplateCategory.SelectedValue.ToString(), ((TableOfContentsWordCategoriesTemplates)(cmbVariableTemplates.SelectedValue)).fileName, lstVariables.SelectedValue.ToString());
                if (variable != null)
                {
                    txtErrorMsg.Text = variable.errorMessage;
                    txtValidationRule.Text = variable.validationRule;
                    ckbCustom.Checked = !string.IsNullOrEmpty(variable.validationRule);
                    ckbRequired.Checked = variable.required;
                    txtErrorMsg.Text = variable.errorMessage;
                    cmbVariableType.DataSource = Constants.VariableType.GetList();
                    cmbVariableType.SelectedIndex = Constants.VariableType.GetList().IndexOf(variable.variableType);
                    txtLabelText.Text = variable.labelText;

                    if (!string.IsNullOrEmpty(variable.initialValues))
                    {
                        var dataSource = variable.initialValues.Split(new string[] { Constants.InitialValueSeperator }, StringSplitOptions.RemoveEmptyEntries).ToList();
                        cmbVariableInitialValues.DataSource = dataSource;
                        cmbVariableInitialValues.SelectedIndex = dataSource.IndexOf(variable.defaultValue);
                    }
                    else
                    {
                        cmbVariableInitialValues.DataSource = new List<string>();
                    }
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }


        private void btnAddVariables_Click(object sender, EventArgs e)
        {
            try
            {
                VariableCreation variableCreation = new VariableCreation(contentProvider.GetSelectedTemplate(cmbVariableTemplateCategory.SelectedValue.ToString(), ((TableOfContentsWordCategoriesTemplates)(cmbVariableTemplates.SelectedValue)).fileName), contentProvider);
                variableCreation.ShowDialog();
                lstVariables.DataSource = contentProvider.GetVariableNamesFromSelectedTemplate(cmbVariableTemplateCategory.SelectedValue.ToString(), ((TableOfContentsWordCategoriesTemplates)(cmbVariableTemplates.SelectedValue)).fileName);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }


        private void ckbCustom_CheckedChanged(object sender, EventArgs e)
        {
            txtErrorMsg.Enabled = ckbCustom.Checked;
            txtValidationRule.Enabled = ckbCustom.Checked;
        }


        private void btnInitVariableValue_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> dataSource = new List<string>();
                if (cmbVariableInitialValues.DataSource != null)
                {
                    dataSource.AddRange((IEnumerable<string>)cmbVariableInitialValues.DataSource);
                }
                StringCollectionEditor editor = new StringCollectionEditor(dataSource);
                editor.ShowDialog();
                cmbVariableInitialValues.DataSource = dataSource;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }

        private void btnDeleteVariables_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Are you sure to delete this variable?", "Alert", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                {
                    contentProvider.DeleteTemplateVariable(cmbVariableTemplateCategory.SelectedValue.ToString(), ((TableOfContentsWordCategoriesTemplates)(cmbVariableTemplates.SelectedValue)).fileName, lstVariables.SelectedItem.ToString());
                    lstVariables.DataSource = contentProvider.GetVariableNamesFromSelectedTemplate(cmbVariableTemplateCategory.SelectedValue.ToString(), ((TableOfContentsWordCategoriesTemplates)(cmbVariableTemplates.SelectedValue)).fileName);
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }

        private void UpdateTemplateVariables()
        {
            string initialValue = string.Empty;
            if (lstVariables.SelectedItem == null)
            {
                return;
            }
            foreach (var item in ((IEnumerable<string>)cmbVariableInitialValues.DataSource))
            {
                initialValue += item + Constants.InitialValueSeperator;
            }
            Variable variable = new Variable
            {
                DefaultValue = cmbVariableInitialValues.SelectedValue != null ? cmbVariableInitialValues.SelectedValue.ToString() : string.Empty,
                ErrorMessage = txtErrorMsg.Text,
                InitialValue = initialValue,
                LableText = txtLabelText.Text,
                Required = ckbRequired.Checked,
                ValidationRule = txtValidationRule.Text,
                VariableType = cmbVariableType.SelectedItem.ToString(),
                VariableName = lstVariables.SelectedItem.ToString()
            };

            contentProvider.UpdateTemplateVariable(cmbVariableTemplateCategory.SelectedValue.ToString(), ((TableOfContentsWordCategoriesTemplates)(cmbVariableTemplates.SelectedValue)).fileName, variable);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateTemplateVariables();
                contentProvider.SaveTemplate();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                contentProvider.SaveTemplate();
                Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }

    }
}
